
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        :permission="permissionList"
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
      >
        <template slot-scope="scope" slot="fileForm">
          <el-upload
            class="upload-demo"
            action="/admin/sys-file/upload"
            :limit="1"
             :headers="headers"
            :on-success="upsucc"
            :on-remove="handleRemove"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </template>
        <template slot="menu" slot-scope="scope">
          <el-button
            v-if="approval_btn"
            icon="el-icon-check"
            size="mini"
            type="text"
            @click="approval"
          >审批</el-button>
          <el-button
            v-if="scope.row.file!=null"
            type="text"
            size="mini"
            icon="el-icon-download"
            @click="download(scope.row)"
          >下载</el-button>
        </template>
        <template slot="menuLeft">
          <el-button type="primary" v-if="submitBatch_btn" size="small" @click="submitBatch">批量提交</el-button>
          <el-button type="primary" v-if="sendBatch_btn" size="small" @click="sendBatch">批量下发</el-button>
          <el-button type="primary" size="small" @click="handleExport">导 出</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  download,
  sendObj,
  submitObj,
  approvalObj
} from "@/api/subwaystm/swstapplynetworkcheckpassengeraffairs";
import { tableOption } from "@/const/crud/subwaystm/swstapplynetworkcheckpassengeraffairs";
import { mapGetters } from "vuex";
import { handleDownFile } from "@/util/util";
import store from '@/store';
import baseCrud from '@/mixins/base-crud'

export default {
  name: "swstapplynetworkcheckpassengeraffairs",
  mixins: [baseCrud('subwaystm/swstapplynetworkcheckpassengeraffairs')],
  data() {
    return {
      fileAddress: "init"
    };
  },

  created() {
    this.dycColumn(this.userInfo.belongToLine);
    this.approval_btn = this.permissions[
      "stm_swstapplynetworkcheckpassengeraffairs_approval"
    ];
    this.sendBatch_btn = this.permissions[
      "stm_swstapplynetworkcheckpassengeraffairs_sendBatch"
    ];
    this.submitBatch_btn = this.permissions[
      "stm_swstapplynetworkcheckpassengeraffairs_submitBatch"
    ];
    console.log(this.userInfo.belongToLine);
  },
  computed: {
    ...mapGetters(["permissions", "userInfo"]),
    fileList(){
      if(this.form.file==null||this.form.file==''){
        return []
      }
      return [{name:this.form.file,url:""}]
    },
        headers() {
		return{
			"Authorization": 'Bearer ' + store.getters.access_token
		}
	},
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.stm_swstapplynetworkcheckpassengeraffairs_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.stm_swstapplynetworkcheckpassengeraffairs_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.stm_swstapplynetworkcheckpassengeraffairs_edit,
          false
        )
      };
    }
  },
  methods: {
    submitBatch: function() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table);
      }
      submitObj(ids)
        .then(data => {
          this.$message.success("提交成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },

    sendBatch: function() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请勾选数据");
        return false;
      }
      let ids = this.$refs.crud.tableSelect;
      sendObj(ids)
        .then(data => {
          this.$message.success("下发成功");
          this.getList(this.page);
          done();
        })
        .catch(() => {
          loading();
        });
    },

    approval: function(row, index, done, loading) {
      approvalObj(row)
        .then(data => {
          this.$message.success("审批成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },

    dycColumn: function(val) {
      var lineCode = this.findObject(this.tableOption.column, "lineCode");
      var stationCode = this.findObject(this.tableOption.column, "stationCode");
      console.log(lineCode);
      if (val != true) {
        console.log("车站");
        lineCode.hide = true;
        stationCode.hide = true;
      } else {
        console.log("线路");
        lineCode.hide = false;
        stationCode.hide = false;
      }
    },
    download: function(row) {
      handleDownFile(row.file);
    },
    handleUpdate: function(row, index, done, loading) {
      if (this.fileAddress != "init") {
        row.file = this.fileAddress;
      }
      console.log(this.fileAddress+"修改成功");
      if (row.applyStatus != "0") {
        this.$message.error("已提交不能修改");
        return false;
      }
      putObj(row)
        .then(data => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    handleSave: function(row, done, loading) {
      if (this.fileAddress != "init") {
        row.file = this.fileAddress;
      }
      console.log(this.fileAddress+"添加成功");
      addObj(row)
        .then(data => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
     upsucc(response, file, fileList) {
      this.fileAddress=response.data.bucketName+"-"+response.data.fileName;
      console.log(this.fileAddress+"上传成功");
    },
    handleRemove(file, fileList) {
      this.fileAddress="";
      console.log(this.fileAddress+"移除成功");
    },
  }
};
</script>
